/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/listener_impl/PublisherListenerImpl.hpp"
#include "dal_rticonnextdds/entity_impl/DalDataWriterImpl.hpp"
#include "dal_rticonnextdds/utils/log.hpp"
#include "dal_rticonnextdds/utils/EntityMappingManager.hpp"

PublisherListenerImpl::PublisherListenerImpl(dal_publisher_listener_callbacks_t callbacks)
{
    this->callbacks_ = callbacks;
    this->rti_pub_listener_ = new DDS_PublisherListener();
    *(this->rti_pub_listener_) = DDS_PublisherListener_INITIALIZER;

    if (this->callbacks_.on_offered_deadline_missed) {
        this->rti_pub_listener_->as_datawriterlistener.on_offered_deadline_missed =
            PublisherListenerImpl::on_offered_deadline_missed;
    } else {
        this->rti_pub_listener_->as_datawriterlistener.on_offered_deadline_missed =
            PublisherListenerImpl::on_offered_deadline_missed_dummy;
    }

    if (this->callbacks_.on_liveliness_lost) {
        this->rti_pub_listener_->as_datawriterlistener.on_liveliness_lost =
            PublisherListenerImpl::on_liveliness_lost;
    } else {
        this->rti_pub_listener_->as_datawriterlistener.on_liveliness_lost =
            PublisherListenerImpl::on_liveliness_lost_dummy;
    }

    if (this->callbacks_.on_offered_incompatible_qos) {
        this->rti_pub_listener_->as_datawriterlistener.on_offered_incompatible_qos =
            PublisherListenerImpl::on_offered_incompatible_qos;
    } else {
        this->rti_pub_listener_->as_datawriterlistener.on_offered_incompatible_qos =
            PublisherListenerImpl::on_offered_incompatible_qos_dummy;
    }

    if (this->callbacks_.on_publication_matched) {
        this->rti_pub_listener_->as_datawriterlistener.on_publication_matched =
            PublisherListenerImpl::on_publication_matched;
    } else {
        this->rti_pub_listener_->as_datawriterlistener.on_publication_matched =
            PublisherListenerImpl::on_publication_matched_dummy;
    }

    this->rti_pub_listener_->as_datawriterlistener.as_listener.listener_data =
        reinterpret_cast<void*>(&this->callbacks_);
}

PublisherListenerImpl::~PublisherListenerImpl()
{
    if (this->rti_pub_listener_) {
        delete this->rti_pub_listener_;
        this->rti_pub_listener_ = nullptr;
    }
}

DDS_PublisherListener* PublisherListenerImpl::getListener()
{
    return this->rti_pub_listener_;
}

void PublisherListenerImpl::on_offered_deadline_missed(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedDeadlineMissedStatus* status)
{
    dal_publisher_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_publisher_listener_callbacks_t*>(listener_data);
    if (callbacks->on_offered_deadline_missed == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedDeadlineMissedStatusFromRtiddsToDal(status,
                                                                                      &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_offered_deadline_missed)(dal_writer, &dal_status, data);
}

void PublisherListenerImpl::on_liveliness_lost(void* listener_data,
                                               DDS_DataWriter* writer,
                                               const DDS_LivelinessLostStatus* status)
{
    dal_publisher_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_publisher_listener_callbacks_t*>(listener_data);
    if (callbacks->on_liveliness_lost == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_liveliness_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessLostStatusFromRtiddsToDal(status, &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_liveliness_lost)(dal_writer, &dal_status, data);
}

void PublisherListenerImpl::on_offered_incompatible_qos(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedIncompatibleQosStatus* status)
{
    dal_publisher_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_publisher_listener_callbacks_t*>(listener_data);
    if (callbacks->on_offered_incompatible_qos == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedIncompatibleQosStatusFromRtiddsToDal(status,
                                                                                       &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_offered_incompatible_qos)(dal_writer, &dal_status, data);
}

void PublisherListenerImpl::on_publication_matched(void* listener_data,
                                                   DDS_DataWriter* writer,
                                                   const DDS_PublicationMatchedStatus* status)
{
    dal_publisher_listener_callbacks_t* callbacks =
        reinterpret_cast<dal_publisher_listener_callbacks_t*>(listener_data);
    if (callbacks->on_publication_matched == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_publication_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertPublicationMatchedStatusFromRtiddsToDal(status,
                                                                                   &dal_status);

    void* data = callbacks->data_;
    (callbacks->on_publication_matched)(dal_writer, &dal_status, data);
}

void PublisherListenerImpl::on_offered_deadline_missed_dummy(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedDeadlineMissedStatus* status)
{
}

void PublisherListenerImpl::on_liveliness_lost_dummy(void* listener_data,
                                                     DDS_DataWriter* writer,
                                                     const DDS_LivelinessLostStatus* status)
{
}

void PublisherListenerImpl::on_offered_incompatible_qos_dummy(
    void* listener_data, DDS_DataWriter* writer, const DDS_OfferedIncompatibleQosStatus* status)
{
}

void PublisherListenerImpl::on_publication_matched_dummy(void* listener_data,
                                                         DDS_DataWriter* writer,
                                                         const DDS_PublicationMatchedStatus* status)
{
}